package mmc.util;

import java.io.IOException;
import java.io.InputStream;

public class ThroughputMeterDebugInputStream extends InputStream{

	private InputStream inner;
	private APSCounter throughputMeter;
	
	public ThroughputMeterDebugInputStream(APSCounter throughputMeter, InputStream inner) {
		this.inner = inner;
		this.throughputMeter = throughputMeter;
	}

	@Override
	public int available() throws IOException {
		return inner.available();
	}

	@Override
	public void close() throws IOException {
		inner.close();
	}

	@Override
	public synchronized void mark(int readlimit) {
		inner.mark(readlimit);
	}

	@Override
	public boolean markSupported() {
		return inner.markSupported();
	}

	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		int bytesRead = inner.read(b, off, len);
		throughputMeter.notify(0, bytesRead);
		return bytesRead;
	}

	@Override
	public int read(byte[] b) throws IOException {
		int bytesRead = inner.read(b);
		throughputMeter.notify(0, bytesRead);
		return bytesRead;
	}

	@Override
	public synchronized void reset() throws IOException {
		inner.reset();
	}

	@Override
	public long skip(long n) throws IOException {
		return inner.skip(n);
	}

	@Override
	public int read() throws IOException {
		int ret = inner.read();
		if(ret!=-1)
			throughputMeter.notify(0, 1);
		return ret;
	}
}
